<template>
  <el-dialog
    title="开通会员"
    top="22vh"
    :visible.sync="dialogVisible"
    close-on-press-escape
    :before-close="cancel"
  >
    <el-table
      v-loading="loading"
      border
      :data="vipList"
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center"
      />
      <el-table-column
        prop="name"
        label="会员名称"
        align="center"
      />
      <el-table-column
        prop="price"
        label="会员价格"
        align="center"
      />
      <el-table-column
        prop="discount"
        label="享受折扣"
        align="center"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="medium"
            type="danger"
            round
            @click="joinVip(scope.$index, scope.row)"
          >开通</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
import { getVipList } from '@/api/vip'
import { modifyCustomerUserType } from '@/api/customer'
import { alipay } from '@/api/pay'
import { Message } from 'element-ui'
export default {
  name: 'AssessDialog',
  props: {
    dialogVisible: Boolean
  },
  data() {
    return {
      vipList: null,
      loading: false
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.loading = true
      getVipList().then(res => {
        if (res) {
          this.vipList = res.data.list
          setTimeout(() => {
            this.loading = false
          }, 2 * 1000)
        }
        this.loading = false
      })
    },
    joinVip(index, row) {
      const obj = {}
      obj.userType = row.vipType
      obj.username = this.$store.getters.name
      modifyCustomerUserType(obj).then(res => {
        if (res) {
          Message({
            message: res.data.message,
            type: 'success',
            duration: 5 * 1000
          })
          this.$store.dispatch('user/changeUserType', row.vipType)
          this.cancel()
          this.$emit('init')
        }
      })
      // const obj = {}
      // obj.orderId = row._id
      // alipay(obj).then(res => {
      //   window.open(res.data.data)
      // })
    },
    cancel() {
      this.$emit('closeDialog')
    }
  }
}
</script>

<style lang="scss">

</style>
